'Option Strict Off
'Option Explicit On
'Module MODexport

'	Private oConn As ADODB.Connection
'	Private oRS As ADODB.Recordset
'	Private fso As Scripting.FileSystemObject
'	Private oTextFile As Scripting.TextStream

'	Dim sFieldsToExport, sTableToExport, sExportPath As String

'	Dim i As Integer

'	' Export to Excel by creating a new workbook and worksheets, then show the Application to the user and save it --------------------------

'	Public Function exportToExcel(ByRef sTableToExport As String, ByRef sFieldsToExport As String, ByRef sExportPath As String) As Boolean

'		On Error Resume Next

'        Dim oExcel As Microsoft.Office.Interop.Excel.Application
'		Dim oWorkbook As Microsoft.Office.Interop.Excel.Workbook
'		Dim oWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

'		Dim iTotalWorkSheets, iWorkSheetsToAdd As Short

'		DLGexport.Cursor = System.Windows.Forms.Cursors.WaitCursor
'		FRMmain.Cursor = System.Windows.Forms.Cursors.WaitCursor
'		DLGexport.CMDExport.Enabled = False

'		If Not openRecordset(sTableToExport, sFieldsToExport) Then

'			Exit Function

'		End If

'		'UPGRADE_WARNING: Couldn't resolve default property of object DLGexport.ProgressBar.Visible. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'		DLGexport.ProgressBar.Visible = True

'		'UPGRADE_WARNING: Couldn't resolve default property of object DLGexport.ProgressBar.Value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'		DLGexport.ProgressBar.Value = 33

'		' Create a new Excel object ---------------------------------------------------------------------------------------------

'		oExcel = New Microsoft.Office.Interop.Excel.Application

'		System.Windows.Forms.Application.DoEvents()

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not create new Excel application",  , Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not create new Excel application")

'			GoTo exitSub

'		Else

'			logMessage(5, "MODexport", "Created Excel application object")

'		End If

'		oWorkbook = oExcel.Workbooks.Add

'		System.Windows.Forms.Application.DoEvents()

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not add workbook",  , Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not add workbook")

'			GoTo exitSub

'		Else

'			logMessage(4, "MODexport", "Added workbook to Excel file")

'		End If

'		iTotalWorkSheets = Int(oRS.RecordCount / 65000) + 1

'		On Error Resume Next

'		If oWorkbook.Worksheets.Count < iTotalWorkSheets Then

'			iWorkSheetsToAdd = iTotalWorkSheets - oWorkbook.Worksheets.Count

'			oWorkbook.Worksheets.Add( ,  , iWorkSheetsToAdd)

'			System.Windows.Forms.Application.DoEvents()

'			If Err.Number <> 0 Then

'				logMessage(1, "MODexport", "Could not add worksheet",  , Err.Number, Err.Description)

'				MsgBox("Could not export" & vbCrLf & "Reason : Could not add worksheet")

'				GoTo exitSub

'			Else

'				logMessage(4, "MODexport", "Added worksheet to Excel file", "Worksheets added = " & iWorkSheetsToAdd)

'			End If

'		End If

'		Dim j As Short
'		For i = 1 To iTotalWorkSheets



'			'UPGRADE_WARNING: Couldn't resolve default property of object DLGexport.ProgressBar.Value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'			DLGexport.ProgressBar.Value = i / iTotalWorkSheets * 100

'			oWorkSheet = oWorkbook.Worksheets(i)

'			For j = 1 To oRS.Fields.Count

'				'UPGRADE_WARNING: Couldn't resolve default property of object oWorkSheet.Cells(1, j).Value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'				oWorkSheet.Cells._Default(1, j).Value = oRS.Fields(j - 1).Name

'				System.Windows.Forms.Application.DoEvents()

'			Next 

'			System.Windows.Forms.Application.DoEvents()

'			'UPGRADE_WARNING: Couldn't resolve default property of object oWorkSheet.Cells().CopyFromRecordset. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'			oWorkSheet.Cells._Default(2, 1).CopyFromRecordset(oRS, 65000)

'			System.Windows.Forms.Application.DoEvents()

'			If Err.Number <> 0 Then

'				logMessage(1, "MODexport", "Could not copy from recordset",  , Err.Number, Err.Description)

'				MsgBox("Could not export" & vbCrLf & "Reason : Could not copy from recordset")

'				GoTo exitSub

'			Else

'				logMessage(4, "MODexport", "Copied records from recordset to Excel")

'			End If

'			oWorkSheet.Name = sTableToExport & "_" & i

'		Next 

'		' Make Excel visible -------------------------------------------------------------------------------------------------------------------

'		'oExcel.Visible = True
'		'oExcel.UserControl = True

'		' Save the Workbook -------------------------------------------------------------------------------------------------------------

'		On Error Resume Next

'		System.Windows.Forms.Application.DoEvents()

'		oWorkbook.SaveAs(sExportPath)

'		System.Windows.Forms.Application.DoEvents()

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not save Excel file", "Path = " & sExportPath, Err.Number, Err.Description)

'			MsgBox("Could not save Excel file")

'			GoTo exitSub

'		Else

'			logMessage(3, "MODexport", "Saved Excel file", "Path = " & sExportPath)

'		End If

'		exportToExcel = True

'		MsgBox("Finished exporting")

'exitSub: 

'		DLGexport.Cursor = System.Windows.Forms.Cursors.Arrow
'		FRMmain.Cursor = System.Windows.Forms.Cursors.Arrow

'		oRS.Close()
'		oConn.Close()

'		'UPGRADE_NOTE: Object oRS may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oRS = Nothing
'		'UPGRADE_NOTE: Object oConn may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oConn = Nothing

'		oExcel.Quit()

'		'UPGRADE_NOTE: Object oExcel may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oExcel = Nothing
'		'UPGRADE_NOTE: Object oWorkbook may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oWorkbook = Nothing
'		'UPGRADE_NOTE: Object oWorkSheet may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oWorkSheet = Nothing

'	End Function

'	' Export records to a text file -----------------------------------------------------------------------------------------------------

'	Public Function exportToText(ByRef sTableToExport As String, ByRef sFieldsToExport As String, ByRef sExportPath As String) As Object

'		On Error Resume Next

'		Dim sLine, sFieldValue As String

'		Dim lTotalRecords As Integer

'		Dim oField As ADODB.Field

'		'UPGRADE_WARNING: Couldn't resolve default property of object DLGexport.ProgressBar.Visible. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'		DLGexport.ProgressBar.Visible = True
'		DLGexport.CMDExport.Enabled = False
'		DLGexport.CMDcancel.Enabled = False
'		FRMmain.Cursor = System.Windows.Forms.Cursors.WaitCursor
'		DLGexport.Cursor = System.Windows.Forms.Cursors.WaitCursor

'		If Not openRecordset(sTableToExport, sFieldsToExport) Then

'			GoTo exitFunction

'		End If

'		On Error Resume Next

'		fso = New Scripting.FileSystemObject

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not create FileSystem object for exporting to text file",  , Err.Number, Err.Description)

'			GoTo exitFunction

'		Else

'			logMessage(5, "MODexport", "Created FileSystem object for exporting to text file")

'		End If


'		If fso.FileExists(sExportPath) Then

'			If MsgBox("A file with the name """ & sExportPath & """ already exists" & vbCrLf & "Would you like to replace it ?", MsgBoxStyle.OKCancel) <> 1 Then

'				GoTo exitFunction

'			End If

'		End If

'		oTextFile = fso.CreateTextFile(sExportPath, True)

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not create text file object for exporting to text file",  , Err.Number, Err.Description)

'			'UPGRADE_NOTE: Object fso may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'			fso = Nothing

'			GoTo exitFunction

'		Else

'			logMessage(5, "MODexport", "Created text file for exporting to text file")

'		End If

'		For	Each oField In oRS.Fields

'			sLine = sLine & oField.Name & vbTab

'		Next oField

'		oTextFile.WriteLine(sLine)

'		sLine = ""

'		i = 0

'		lTotalRecords = oRS.RecordCount

'		' Loop through records and write to file ----------------------------------------------------------------------------------------

'		Do Until oRS.EOF

'			sLine = ""

'			For	Each oField In oRS.Fields

'				sFieldValue = oField.Value

'				' replace commas, because they are used to seperate the fields ------------------------------------------------------------

'				If sFieldValue <> "" Then

'					sFieldValue = Replace(sFieldValue, ",", "", 1,  , CompareMethod.Text)

'				End If

'				sLine = sLine & sFieldValue & ","

'			Next oField

'			' remove the last comma ----------------------------------------------------------------------------------------------------

'			sLine = Left(sLine, Len(sLine) - 1)

'			On Error Resume Next

'			oTextFile.WriteLine(sLine)

'			If Err.Number <> 0 Then

'				logMessage(2, "MODexport", "Could not write line to text file", "Line = " & sLine, Err.Number, Err.Description)

'				Err.Clear()

'			End If

'			System.Windows.Forms.Application.DoEvents()

'			i = i + 1

'			'UPGRADE_WARNING: Couldn't resolve default property of object DLGexport.ProgressBar.Value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'			DLGexport.ProgressBar.Value = i / lTotalRecords * 100

'			oRS.MoveNext()

'		Loop 

'		oTextFile.Close()

'		'UPGRADE_NOTE: Object oTextFile may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		oTextFile = Nothing
'		'UPGRADE_NOTE: Object fso may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
'		fso = Nothing

'		MsgBox("Finished exporting")

'		DLGexport.Close()

'exitFunction: 

'		DLGexport.CMDExport.Enabled = True
'		DLGexport.CMDcancel.Enabled = True
'		FRMmain.Cursor = System.Windows.Forms.Cursors.Arrow
'		DLGexport.Cursor = System.Windows.Forms.Cursors.Arrow

'	End Function

'	' Create a connectioon and open a recordet --------------------------------------------------------------------------------------

'	Private Function openRecordset(ByRef sTableToExport As String, ByRef sFieldsToExport As String) As Boolean

'		On Error Resume Next

'		Dim sRSOpen As String

'		oConn = New ADODB.Connection

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not create ADODB connection object",  , Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not create connection to database")

'			Exit Function

'		Else

'			logMessage(5, "MODexport", "Created new ADODB connection object")

'		End If

'		oRS = New ADODB.Recordset

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not create ADODB recordset object",  , Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not create connection to database recordset")

'			Exit Function

'		Else

'			logMessage(5, "MODexport", "Created ADODB recordset object")

'		End If

'		Dim sConnection As String

'		'UPGRADE_WARNING: Couldn't resolve default property of object FRMmain.Adodc1. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
'		sConnection = FRMmain.Adodc1.ConnectionString

'		oConn.Open(sConnection)

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not open ADODB connection", "Connection string = " & sConnection, Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not open connection to database")

'			Exit Function

'		Else

'			logMessage(4, "MODexport", "Opened ADODB connection", "Connection string = " & sConnection)

'		End If

'		sRSOpen = "SELECT " & sFieldsToExport & " FROM " & sTableToExport & " ORDER BY [uniqueID]"

'		On Error Resume Next

'		oRS.Open(sRSOpen, oConn, 1, 3)

'		If Err.Number <> 0 Then

'			logMessage(1, "MODexport", "Could not open ADODB recordset", "Recordset open string = " & sRSOpen, Err.Number, Err.Description)

'			MsgBox("Could not export" & vbCrLf & "Reason : Could not open recordset")

'			Exit Function

'		Else

'			logMessage(4, "MODexport", "Opened ADODB recordset", "Recordset open string = " & sRSOpen)

'		End If

'		openRecordset = True

'	End Function


'	'Public Function export(tableToExport As String, strFieldsToExport As String, strExportAs As String, strExportMethod As String)
'	'
'	'    On Error Resume Next
'	'
'	'    FRMmain.MousePointer = 11
'	'    DLGexport.MousePointer = 11
'	'
'	'    Dim conn As ADODB.Connection
'	'    Dim r As ADODB.Recordset
'	'    Dim openStr As String
'	'    Dim strConn As String
'	'
'	'    Set conn = New ADODB.Connection
'	'    Set r = New ADODB.Recordset
'	'    Set fso = New Scripting.FileSystemObject
'	'
'	'    strConn = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & theFileName
'	'    conn.Open strConn
'	'
'	'    openStr = "SELECT " & strFieldsToExport & " FROM " & tableToExport
'	'    r.Open openStr, conn, 1, 3
'	'
'	'    Dim longFileRow As Long
'	'    Dim intFileNumber As Integer
'	'    Dim intNumberOfFiles As Integer
'	'    Dim longNumberOfRecords As Long
'	'    Dim strHeader As String
'	'    Dim strLine As String
'	'    intFileNumber = 1
'	'    longNumberOfRecords = r.RecordCount
'	'    intNumberOfFiles = Int(r.RecordCount / 65000) + 1
'	'
'	'    If strExportMethod = "excel" Then
'	''-------------------------------------------------------------------'
'	''   Export to Excel                                                 '
'	''-------------------------------------------------------------------'
'	'        For i = 1 To intNumberOfFiles
'	'            strExportAs = Replace(strExportAs, ".xls", "", , , vbTextCompare)
'	'            Set excelFile = fso.CreateTextFile(App.Path & "/" & strExportAs & intFileNumber & ".xls", True, True)
'	'            header = ""
'	'            For Each fld In r.Fields
'	'                header = header & fld.Name & Chr(9)
'	'            Next
'	'            excelFile.WriteLine header
'	'            Do While longFileRow < 65001 And Not r.EOF
'	'                strLine = ""
'	'                For Each fld In r.Fields
'	'                    strLine = strLine & fld.Value & Chr(9)
'	'                Next
'	'                excelFile.WriteLine strLine
'	'                longFileRow = longFileRow + 1
'	'                r.MoveNext
'	'            Loop
'	'            longFileRow = 0
'	'            excelFile.Close
'	'            Set excelFile = Nothing
'	'            intFileNumber = intFileNumber + 1
'	'        Next
'	'    Else
'	''-------------------------------------------------------------------'
'	''   Export to Text File                                             '
'	''-------------------------------------------------------------------'
'	'        intNumberOfFiles = 1
'	'        Dim textFile As Object
'	'        Set textFile = fso.CreateTextFile(App.Path & "/" & strExportAs, True, True)
'	'        Dim line As String
'	'        For Each fld In r.Fields
'	'            header = header & fld.Name & Chr(9)
'	'        Next
'	'            textFile.WriteLine header
'	'        Do Until r.EOF
'	'            line = ""
'	'            For Each fld In r.Fields
'	'                Dim fieldVal
'	'                If fld.Value <> "" And InStr(fld.Value, ",") <> 0 Then
'	'                    fieldVal = Replace(fld.Value, ",", "", , , vbTextCompare)
'	'                Else
'	'                    fieldVal = fld.Value
'	'                End If
'	'                line = line & fieldVal & ","
'	'            Next
'	'            textFile.WriteLine line
'	'            r.MoveNext
'	'        Loop
'	'        textFile.Close
'	'        Set textFile = Nothing
'	'        Set fso = Nothing
'	'    End If
'	'doStop:
'	'    r.Close
'	'    conn.Close
'	'
'	'    Set r = Nothing
'	'    Set conn = Nothing
'	'
'	'    Set fs = Nothing
'	'    Set excelFile = Nothing
'	'    FRMmain.MousePointer = 0
'	'    DLGexport.MousePointer = 0
'	'    MsgBox "Finished Exporting " & intNumberOfFiles & " Files"
'	'End Function
'End Module